package client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Scanner;

import util.userObject;

public class ClientController {

	private int guessCount = 10;
	private ClientMastermind game;
	private String username, password;
	private String firstName, lastName, phoneNo;
	String loginSuccess = null;
	private Scanner input = new Scanner(System.in);
	private ObjectInputStream inputStream;
	private ObjectOutputStream outputStream;


	public ClientController(ClientMastermind game){
		this.game = game;
		try{
			/*establish streams with server */
			inputStream = new ObjectInputStream(game.getSocket().getInputStream());
			outputStream = new ObjectOutputStream(game.getSocket().getOutputStream());
		}
		catch(IOException e){
			System.out.println(e);
		}
		String ping = null;
		try {
			ping = (String) inputStream.readObject();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		System.out.println(ping);
		/*choose what to do */
		System.out.println("Enter an option: ");
		System.out.println("1. Sign in existing user.");
		System.out.println("2. Register new user.");
		System.out.println("3. Exit");
		switch(input.nextInt()){
		case 1 : signIn();
		break;
		case 2 : register();
		break;
		case 3 : quit();
		break;
		default : System.out.println("Error incorrect input.");
		}
	}

	private void signIn(){
		System.out.print("Enter username: ");
		input = new Scanner(System.in);
		username = input.nextLine();
		System.out.println("Enter password: ");
		password = input.nextLine();
		userObject signin = new userObject("LOG", username, password);
		try{
			outputStream.writeObject(signin);
			try {
				loginSuccess = (String) inputStream.readObject();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			if(loginSuccess.equals("LOGGEDIN")){
				System.out.println("Login Success!");
				waitForMove();
			} else {
				System.out.println("Login Failed!");
			}
		}
		catch(IOException e){
			System.out.println(e);
		}

	}

	private void register(){
		input = new Scanner(System.in);
		System.out.print("Enter desired Username: ");
		username = input.nextLine();
		System.out.println("Enter desired Password: ");
		password = input.nextLine();
		System.out.print("Enter First Name: ");
		firstName = input.nextLine();
		System.out.print("Enter Last Name: ");
		lastName = input.nextLine();
		System.out.print("Enter Phone Number: ");
		phoneNo = input.nextLine();
		userObject register = new userObject("REG", username, password, firstName, lastName, phoneNo);
		try{
			outputStream.writeObject(register);
			try {
				System.out.print(inputStream.readObject());
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		catch(IOException e){
			System.out.println(e);
		}
	}

	/**
	 * After user is logged in, display welcome details and start game
	 * @author dylan
	 */
	private void initialiseGame(){
		System.out.println("Welcome " + username + ".");
		System.out.println("You have" + guessCount + "moves to complete this game.");
		System.out.println("Feedback will be given for each move.");
		waitForMove();
	}

	/**
	 * Wait for the first move from the user, once recieved check it and send it.
	 * @author dylan
	 */
	private void waitForMove(){
		String inputString;
		System.out.println("Your Guess: ");
		do{
			boolean preSend = false;
			do {
				if(preSend == true){
					System.out.println("First Move: ");
				} else {
					System.out.println("Retry Move: ");
				}
				input = new Scanner(System.in);
				inputString = input.nextLine();
				preSend = validateMove(inputString);
			}while(!preSend);
			try {
				outputStream.writeObject(inputString);
				String feedback = null;
				try {
					feedback = (String) inputStream.readObject();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
				System.out.println("Your feedback: " + feedback);
				guessCount--;
			} catch (IOException e) {
				System.out.println(e);
			}
		}while(guessCount != 0);
	}

	private Boolean validateMove(String moveSequence){
		boolean result = true;
		if(moveSequence.length() != 6){
			System.out.println("Please enter 6 colours!");
			result = false;
		} else if(!moveSequence.matches("^[RGBYOW]+$")){
			System.out.println("Please only enter characters RGBYOW!");
			result = false;
		}
		return result;
	}

	private void gameOver(){
		System.out.println("Thanks for playing!");
		quit();
	}

	/**
	 * Closes streams and exits the system
	 * @author naggy
	 */
	private void quit(){
		try{
			this.inputStream.close();
			this.outputStream.close();
		}
		catch(IOException e){
			System.out.println(e);
		}
		System.exit(1);
	}


}
